package com.zzyl.nursing.service.impl;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.util.ObjectUtil;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.common.utils.StringUtils;
import com.zzyl.framework.web.service.TokenService;
import com.zzyl.nursing.dto.UserLoginRequestDto;
import com.zzyl.nursing.service.WechatService;
import com.zzyl.nursing.vo.LoginVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.FamilyMemberMapper;
import com.zzyl.nursing.domain.FamilyMember;
import com.zzyl.nursing.service.IFamilyMemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * 老人家属Service业务层处理
 *
 * @author alexis
 * @date 2025-10-11
 */
@Service
public class FamilyMemberServiceImpl extends ServiceImpl<FamilyMemberMapper, FamilyMember> implements IFamilyMemberService {
    @Autowired
    private FamilyMemberMapper familyMemberMapper;

    @Autowired
    private WechatService wechatService;

    @Autowired
    private TokenService tokenService;

   static List<String> DEFAULT_NICKNAME_PREFIX = ListUtil.of(
            "生活更美好",
            "大桔大利",
            "日富一日",
            "好柿开花",
            "柿柿如意",
            "一椰暴富",
            "大柚所为",
            "杨梅吐气",
            "天生荔枝"
            );

    @Override
    public LoginVo Login(UserLoginRequestDto userLoginRequestDto) {
        //获取openid
        String openid = wechatService.getOpenid(userLoginRequestDto.getCode());
        //根据openid查询家属信息
        FamilyMember familyMember = lambdaQuery().eq(FamilyMember::getOpenId, openid).one();
        //如果用户为空赋值openid
        if (familyMember == null) {
            familyMember = new FamilyMember();
            familyMember.setOpenId(openid);
        }
        //根据PhoneCode查询手机号
        String phone = wechatService.getPhone(userLoginRequestDto.getPhoneCode());
        //保存或更新家属信息
        insertOrUpdate(familyMember, phone);
        //获取token并返回
        LoginVo loginVo = new LoginVo();
        Map<String, Object> claims = new HashMap<>();
        claims.put("userId", familyMember.getId());
        claims.put("nickName", familyMember.getName());
        loginVo.setToken(tokenService.createToken(claims));
        loginVo.setNickName(familyMember.getName());
        return loginVo;
    }

    /**
     * 保存或更新
     *
     * @param familyMember
     */
    private void insertOrUpdate(FamilyMember familyMember, String phone) {
        //如果id不为空且手机号相同则返回
        if (familyMember.getId() != null && familyMember.getPhone().equals(phone)) {
            return;
        }
        //如果id不为空且手机号不同则更新手机号
        if (familyMember.getId() != null && ObjectUtil.notEqual(phone, familyMember.getPhone())) {
            familyMember.setPhone(phone);
            updateById(familyMember);
        }
        //如果id为空则生成昵称和设置手机号
        if(familyMember.getId() == null){
            String nickName = DEFAULT_NICKNAME_PREFIX.get((int) (Math.random() * DEFAULT_NICKNAME_PREFIX.size()))
                    + StringUtils.substring(phone, 7);
            familyMember.setName(nickName);
            familyMember.setPhone(phone);
            save(familyMember);
        }
    }
}
